package sample.java.controller;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import sample.java.controller.center.HomeCentreLocalController;
import sample.java.controller.jiekou.TransData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import javafx.util.Duration;

import javax.annotation.Resource;
import javax.swing.*;
import javax.xml.ws.AsyncHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Controller {
    @FXML
    private BorderPane homeBorderPane;
    private FXMLLoader defualLoader;
    private FXMLLoader recentLoader;
    private FXMLLoader saveLoader;
    private FXMLLoader localLoader;
    private MediaPlayer mplayer;

    private Node dd;
    private Node rr;
    private Node ss;
    private Node ll;

    @FXML
    private Label songName;
    @FXML
    private Label songTime;
    @FXML
    private ImageView ivPlay;

    private double allTime;//当前音乐总时长
    private double currentTime;
    private String currentPath;

    private HomeCentreLocalController localController;

    @FXML
    public void showDefualList() throws IOException {
        if (defualLoader == null) {
            defualLoader = new FXMLLoader(getClass().getResource("/xml/home_centre_defual.fxml"));
        }

        if (dd == null) {
            dd = defualLoader.load();
        }
        homeBorderPane.setCenter(dd);
    }

    @FXML
    public void showRecentList() throws IOException {
        if (recentLoader == null) {
            recentLoader = new FXMLLoader(getClass().getResource("/xml/home_centre_recently.fxml"));
        }

        if (rr == null) {
            rr = recentLoader.load();
        }
        homeBorderPane.setCenter(rr);
    }

    @FXML
    public void showSaveList() throws IOException {
        if (saveLoader == null) {
            saveLoader = new FXMLLoader(getClass().getResource("/xml/home_centre_save.fxml"));
        }

        if (ss == null) {
            ss = saveLoader.load();
        }
        homeBorderPane.setCenter(ss);
    }

    @FXML
    public void showLocalList() throws IOException {
        if (localLoader == null) {
            localLoader = new FXMLLoader(getClass().getResource("/xml/home_centre_local.fxml"));
        }

        if (ll == null) {
            ll = localLoader.load();
        }
        homeBorderPane.setCenter(ll);
        if (localController == null) {
            localController = (HomeCentreLocalController) localLoader.getController();

            localController.setTransData(new TransData<String, String>() {
                @Override
                public void passData(String s, String s2) {
                    currentPath = s;
                    pp(s);
                }
            });
        }
    }

    @FXML
    public void playMusic() throws Exception {
        if (currentPath == null) {
            currentPath = "D:///KuGou///kkk.mp3";
        }
        pp(currentPath);
    }

    public void pp(String path) {
        File ff = new File(path);
        Media media = new Media(ff.toURI().toString());

        if ( mplayer != null&&mplayer.getStatus() == MediaPlayer.Status.PLAYING) {
            mplayer.stop();
            mplayer.dispose();
            mplayer = null;
        }

        mplayer = new MediaPlayer(media);


        ivPlay.setImage(new Image("/img/bofang.png"));

        mplayer.setOnReady(new Runnable() {
            @Override
            public void run() {
                Duration duration = media.getDuration();
                allTime = duration.toSeconds();
                String[] lujing = media.getSource().split("/");
                String name = lujing[lujing.length - 1];
                songName.setText(name);
                songTime.setText("00:00/" + Seconds2Str(allTime));
            }
        });
        mplayer.currentTimeProperty().addListener(new ChangeListener<Duration>() {
            @Override
            public void changed(ObservableValue<? extends Duration> observable, Duration oldValue, Duration newValue) {
                currentTime = newValue.toSeconds();
                songTime.setText(Seconds2Str(currentTime) + "/" + Seconds2Str(allTime));
            }
        });
        mplayer.setOnEndOfMedia(new Runnable() {
            @Override
            public void run() {
                ivPlay.setImage(new Image("/img/zanting.png"));
                songTime.setText(Seconds2Str(allTime) + "/" + Seconds2Str(allTime));
            }
        });
        mplayer.play();
    }

    private String Seconds2Str(Double seconds) {
        Integer count = seconds.intValue();
        int ff = count / 60;
        int mm = count % 60;
        String f = "";
        String m = "";
        if (ff < 10) {
            f = "0" + ff;
        } else {
            f = ff + "";
        }

        if (mm < 10) {
            m = "0" + mm;
        } else {
            m = "" + mm;
        }

        return f + ":" + m;
    }

}
